package com.cy.store.Intercepter;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 登录拦截器
 * 返回值为true放行,false拦截
 * @author 谭杰
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response
                            , Object handler) throws Exception {

        //通过request对象获取session对象
        Object obj=request.getSession().getAttribute("uid");
        if(obj==null){
            //说明用户没有登录,于是重定向到login.html
            response.sendRedirect("/web/login.html");
            //结束后续的调用
            return false;
        }
        else {
            //放行
            return true;
        }
    }
}
